﻿// JavaScript Document

function slider_image(container, target) {
	this.container = $E(container);
	this.target = $E(target);
	this.imgs = [];
	this.imgsd = [];
	this.eids = [];
	this.layer = null;
	this.width = 110;
	//
	this.init = function(){
		var imgs = this.container.getElementsByTagName("IMG");
		for(var i=0; i<imgs.length; i++) {
			//var img = new Image();
			//img.src = imgs[i].src.replace('thumbs', 'resized');
			//var eid = dgetAttr(imgs[i], "eid");
			//this.imgs[eid] = img;
			var eid = dgetAttr(imgs[i], "eid");
			this.eids.push(eid);
			this.imgs[eid] = imgs[i].src.replace('thumbs', 'resized');
			this.imgsd[eid] = null;
		}
		this.length = imgs.length;
		//this.dlayer();
		//
		//var t = Math.floor(this.length%10);
		//var c = '';
		//for(var i=0; i+1<t; i++) {
		//	c += '<a href="javascript:;" onclick="">'+(i*10+1)+'</a> ';
		//}
		//$E("gnav").innerHTML = c;
	};
	this.start = function(){
		var eid = this.eids[0];
		this.click(eid);
	};
	this.click = function(eid) {
		//dmask(40);
		//this.layer_content.appendChild(this.imgs[eid]);
		//dmaskd();
		if( this.target == null ) {
			//
		} else {
			//this.target.src = this.imgs[eid].src;
			//this.target.src = this.imgs[eid];
			var _obj = this;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			//
			if( this.imgsd[eid] != null ) {
				this.target.src = this.imgsd[eid].src;
			} else {
				var img = new Image();
				img.src = this.imgs[eid];
				img.onload = function(){
					_obj.target.src = _obj.imgs[eid];
					if( _obj.target.parentNode.tagName.toUpperCase() == "A" ) {
						//_obj.target.parentNode.href = _obj.target.src.replace('resized', 'origins');
					}
					//var psc = _obj._pscount(eid);
					//$E("gtxt").innerHTML = (psc+1) + " of " + _obj.length;
					//var t = Math.max(0, (psc-3) * _obj.width);
					//_obj.container.scrollLeft = t;
					//_obj._move(_obj.container.scrollLeft, t);
				};
			}
			var psc = _obj._pscount(eid);
			$E("gtxt").innerHTML = (psc+1) + " of " + _obj.length;
			var t = Math.max(0, (psc-2) * _obj.width);
			_obj._move(_obj.container.scrollLeft, t);
			
			//
			this._preload(eid);
		}
	};
	this._preload = function(eid){
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				for(var j=Math.max(0, i-4); j<Math.min(i+4, this.eids.length); j++){
					var _eid = this.eids[j];
					if( this.imgsd[_eid] != null ) {
						continue;	
					}
					var pimg = new Image();
					pimg.eid = _eid;
					pimg.src = this.imgs[_eid];
					var _obj = this;
					pimg.onload = function(){
						_obj.imgsd[this.eid] = this;
					};
				}
				break;
			}
		}
	};
	this._pscount = function(eid) {
		var c = 0;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				break;
			}
			c++;
		}
		return c;
	};
	this._psprevious = function(eid, s) {
		var peid = eid;
		if( !s ) s = 1;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				if( i-s >= 0 ) {
					peid = this.eids[i-s];
					break;
				} else {
					peid = this.eids[0];
					break;
				}
			}
		}
		return peid;
	};
	this._psnext = function(eid,s) {
		var neid = eid;
		if( !s ) s = 1;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				if( i+s < this.eids.length ) {
					neid = this.eids[i+s];
					break;
				} else {
					neid = this.eids[this.eids.length-1];
					break;
				}
			}
		}
		return neid;
	};
	this._move = function(c, t) {
		var _obj = this;
		if( t >= _obj.container.scrollWidth - _obj.container.offsetWidth ) {
			_obj.container.scrollLeft = _obj.container.scrollWidth - _obj.container.offsetWidth;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		} else if( t <= 0 ) {
			_obj.container.scrollLeft = 0;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		}
		var s = 10;
		if( Math.abs(c-t) > 200 ) {
			s = 20;
		}
		if( _obj.container.scrollLeft > t + s ) {
			_obj.container.scrollLeft -= s;
		} else if( _obj.container.scrollLeft < t - s ) {
			_obj.container.scrollLeft += s;
		} else {
			_obj.container.scrollLeft = t;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		}
		if( _obj._timer == null ) {
			_obj._timer = window.setInterval(function(){_obj._move(c, t);}, 10);
		}
	};
	this.effect = function(){
		
	};
	this.dlayer = function(){
		
	};
	//
	if( !$E(container) ) {
		return false;
	}
	//
	this.init();
}

//
function slider_hs(tc) {
	this.timer = null;
	this.ts = 40;
	this.da = null;
	this.db = null;
	this.s = 0;
	this.init = function(tc) {
		this.da = $E(tc);
		this.db = appendDIV(this.da.parentNode, "", "", this.da.innerHTML);
	};
	this.start = function(){
		
	};
	//
	this.init(tc);
}